package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.adapter.BallGroupAdapter;
import com.palm.hubo2o.view.GridItemDecoration;
import com.palm.palmutils.DrawableUtils;
import com.palm.palmutils.LayoutUtils;
import com.palm.palmutils.widget.BallView;
import com.palm.palmutils.widget.EasyRecyclerView;

import butterknife.Bind;
import butterknife.BindColor;

/**
 * Created by dest1 on 2015/10/13.
 */
public abstract class MostLotteryFragment extends LotteryFragment implements EasyRecyclerView.OnItemClickListener, View.OnClickListener, EasyRecyclerView.CustomChoiceListener {
    @Bind(R.id.recycler_view)
    EasyRecyclerView mGrid;
    private GridLayoutManager manager;
    @BindColor(R.color.blue)
    int blue;
    @BindColor(R.color.red)
    int red;


    @Override
    protected void onClearChoices() {
        mGrid.clearChoices();
    }


    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
        mGrid.setChoiceMode(EasyRecyclerView.CHOICE_MODE_MULTIPLE_CUSTOM);
        manager = new GridLayoutManager(getContext(), 8);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                int type = mGrid.getAdapter().getItemViewType(position);
                return type == BallGroupAdapter.ITEM_TYPE.ITEM_TYPE_MARK.ordinal() ? manager.getSpanCount() : 1;
            }
        });
        int padding = LayoutUtils.dp2pix(getContext(), 3);
//        mGrid.setSelector(RippleSalon.generateRippleDrawable(AppConfig.screenWidth / (8 * 2) - padding, false));
        mGrid.setClipToPadding(false);
        mGrid.setPadding(padding * 2, padding * 2, padding * 2, padding * 2);
        mGrid.addItemDecoration(new GridItemDecoration(padding));
        mGrid.setLayoutManager(manager);
        mGrid.setAdapter(adapter);
        mGrid.setCustomCheckedListener(this);
        mGrid.setOnItemClickListener(this);
        mGrid.intoCustomChoiceMode();//开启选中模式
    }

    @Override
    public void onDestroyView() {
        /*bug of recyclerview and GridLayoutManager with SpanSizeLookup*/
        ((GridLayoutManager) mGrid.getLayoutManager()).setSpanSizeLookup(null);
        mGrid.setLayoutManager(null);
        mGrid.setAdapter(null);
        super.onDestroyView();
    }

    @Override
    public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
        parent.toggleItemChecked(position);
        return true;
    }

    /**
     * @param format  球数字格式化
     * @param offset  球数字偏移量
     * @param numbers 球组
     */
    protected void setupBalls(String format, int offset, int... numbers) {
        adapter.setOffset(offset);
        adapter.setBallFormat(format);
        adapter.setBallNumber(numbers);
    }


    protected abstract void onSetMarkText(View parent, int groupIndex);


    protected abstract int onSetGroupColor(int groupIndex);


    protected abstract boolean onBallChecked(EasyRecyclerView parent, boolean checked, int position, String ballText, int index);

    protected BallGroupAdapter getAdapter() {
        return adapter;
    }

    private BallGroupAdapter adapter = new BallGroupAdapter() {

        @Override
        public BallHolder onCreateItemHolder(ViewGroup parent) {
//            View ball = LayoutInflater.from(parent.getContext()).inflate(R.layout.ballview, parent, false);
            BallView ball = new BallView(parent.getContext());
            return new BallHolder(ball);
        }

        @Override
        public BallHolder onCreateLabelHolder(ViewGroup parent) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lottery_mark, parent, false);
            TextView random = (TextView) v.findViewById(R.id.random_mark);
            DrawableUtils.setTextViewDrawable(random, R.drawable.ic_shake, Gravity.START);
            random.setOnClickListener(MostLotteryFragment.this);
            v.setEnabled(false);
            return new BallHolder(v);
        }

        @Override
        public void onBindItemViewHolder(BallHolder holder, int groupIndex, int position) {
            BallView ball = (BallView) holder.itemView;
            ball.setNumber(position + getOffset(), getBallFormat());
            ball.setBallColor(onSetGroupColor(groupIndex));
        }

        @Override
        public void onBindMarkViewHolder(BallHolder holder, int groupIndex) {
            if (groupIndex == 0) {
                holder.itemView.findViewById(R.id.random_mark).setVisibility(View.VISIBLE);
            } else {
                holder.itemView.findViewById(R.id.random_mark).setVisibility(View.GONE);
            }
            onSetMarkText(holder.itemView, groupIndex);
        }
    };


    @Override
    public void onClick(View v) {
        onRandomBalls();
    }


    @Override
    public void onIntoCustomChoice(EasyRecyclerView view) {

    }

    @Override
    public void onOutOfCustomChoice(EasyRecyclerView view) {

    }

    @Override
    public void onItemCheckedStateChanged(EasyRecyclerView view, int position, long id, boolean checked) {
        int index = adapter.getItemGroupIndex(position);
        int posInGroup = adapter.getPositionInGroup(position, index);
        BallView ball = (BallView) view.findViewHolderForAdapterPosition(position).itemView;
        if (!onBallChecked(view, checked, posInGroup, ball.getNumber(), index))
            view.setItemChecked(position, false);
    }
}
